package edu.gatech.cs2340.OregonTrail;

import java.awt.Image;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import edu.gatech.cs2340.OregonTrail.EventSystem.CommCoordinator;


public class Location{
	
	private boolean visited;
	private int distance;
	private Region region;// This hasn't been implemented yet
	private String locationName;
	private boolean hasShop;
	private Image image;
	private ItemLoader itemLoader;
	private Shop shop;
	
	
	public Location(String name, int distance, Region region, boolean hasShop, CommCoordinator comm){
		locationName = name;
		this.distance = distance;
		this.region = region;
		this.hasShop = hasShop;

		/* Possibly have unique images for each location
		 * try{
			String filepath = "/edu/gatech/cs2340/OregonTrail/Images/%s.png",locationName;
			image = ImageIO.read(PlayPanel.class.getResource(filepath));
		}
		catch(IOException e){
			
		}*/
		visited = false;
		
		if(hasShop == true){
			itemLoader = new ItemLoader();
			try {
				itemLoader.loadItems("ItemXML.xml");
			} catch (SAXException e) {
				JOptionPane.showMessageDialog(null, "Hey, the parser broke. Fix it");
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				JOptionPane.showMessageDialog(null, "Hey, the parser broke. Fix it. The configuration messed up");
				e.printStackTrace();
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Hey, the parser broke. Fix it. The file couldn't open");
				e.printStackTrace();
			}
			
			if(!itemLoader.getItems().isEmpty()){
				Map<Item, Integer> shopItems = new HashMap<Item, Integer>();
				for(int i = 0; i < itemLoader.getItems().size(); i++){
					shopItems.put((Item)itemLoader.getItems().get(i), Integer.MAX_VALUE);
				}
				shop = new Shop(region, shopItems, comm, locationName);
			}
		}
	}
	
	public int getDistance(){
		return distance;
	}
	
	public boolean hasShop(){
		return hasShop;
	}
	
	public boolean getVisited(){
		return visited;
	}
	
	public void setVisited(boolean visited){
		this.visited = visited;
	}

	public String getLocationName() {
		return locationName;
	}
	
	public Image getImage(){
		return image;
	}
	
	public Region getRegion(){
		return region;
	}
	
	public Shop getShop(){
		return shop;
	}
	
}